package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.*;
import cn.tedu.csmall.product.pojo.VO.AttributeListVO;
import cn.tedu.csmall.product.pojo.VO.AttributeStandardVO;
import cn.tedu.csmall.product.pojo.VO.AttributeTemplateListVO;
import cn.tedu.csmall.product.pojo.VO.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.pojo.dto.AttributeAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateUpdateDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Attribute;
import cn.tedu.csmall.product.pojo.entity.AttributeTemplate;
import cn.tedu.csmall.product.service.IAttributeService;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AttributeServiceImpl implements IAttributeService {

    @Autowired
    private AttributeMapper attributeMapper;
    @Autowired
    private SkuSpecificationMapper skuSpecificationMapper;


    @Override
    public void addNew(AttributeAddNewDTO addNewDTO) {
        log.debug("开始处理添加属性的业务,接受到的参数是:{}",addNewDTO);
        int i = attributeMapper.countByNameAndTemplate(addNewDTO.getName(), addNewDTO.getTemplateId());
        if (i > 0) {
            String message= "添加失败,该名称已被占用";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(addNewDTO,attribute);
        log.debug("准备插入数据");
        int insert = attributeMapper.insert(attribute);
        if (insert != 1){
            String message= "添加错误,当前服务器繁忙,请稍后尝试";
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除属性的业务,删除对象的数据ID是:{}",id);
        AttributeStandardVO standardById = attributeMapper.getStandardById(id);
        if (standardById == null) {
            String message = "删除失败,当前数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int i = skuSpecificationMapper.countByAttributeId(id);
        if (i > 0) {
            String message = "删除失败,当前数据关联着skuSpecification表数据";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
        int i1 = attributeMapper.deleteById(id);
        if (i1 != 1) {
            String message= "添加错误,当前服务器繁忙,请稍后尝试";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }

    }

    @Override
    public void updateInfoById(Long id, AttributeUpdateDTO attributeUpdateDTO) {
        log.debug("开始处理修改属性的业务,修改目标id是:{},修改对象是:{}",id,attributeUpdateDTO);
        AttributeStandardVO standardById = attributeMapper.getStandardById(id);
        if (standardById == null) {
            String message = "修改失败,当前数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        Attribute attribute = new Attribute();
        attribute.setId(id);
        BeanUtils.copyProperties(attributeUpdateDTO,attribute);
        int update = attributeMapper.update(attribute);
        if (update != 1) {
            String message= "修改错误,当前服务器繁忙,请稍后尝试";
            throw new ServiceException(ServiceCode.ERR_UPDATE,message);
        }
    }

    @Override
    public AttributeStandardVO getStandardById(Long id) {
        log.debug("开始处理查询属性数据详情的业务,查询对象的id是:{}",id);
        AttributeStandardVO standardById = attributeMapper.getStandardById(id);
        if (standardById == null) {
            String message = "查询失败,当前数据不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        return standardById;
    }


    @Override
    public List<AttributeListVO> list() {
        log.debug("开始处理查询属性数据详情列表的业务,无参数");
        List<AttributeListVO> list = attributeMapper.list();
        for (Object lists : list){
            log.debug("属性数据:{}",lists);
        }
        return list;
    }


}
